﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace WebServer
{
    /// <summary>
    /// Responds to any request with the same static content
    /// </summary>
    public class StaticContentHandler : BaseRequestHandler
    {
        private byte[] _response;
        /// <summary>
        /// Constructs a new StaticContentHandler with the specified response
        /// </summary>
        /// <param name="response">The response to be returned</param>
        public StaticContentHandler(String response)
        {
            _response = Encoding.UTF8.GetBytes(response);
        }
        /// <summary>
        /// Handles HTTP get
        /// </summary>
        /// <param name="request">The request object</param>
        /// <param name="response">The response object</param>
        public override void Get(HttpListenerRequest request, HttpListenerResponse response)
        {
            response.ContentLength64 = _response.Length;
            response.OutputStream.Write(_response, 0, _response.Length);            
        }

        /// <summary>
        /// Handles HTTP put
        /// </summary>
        /// <param name="request">The request object</param>
        /// <param name="response">The response object</param>
        public override void Put(HttpListenerRequest request, HttpListenerResponse response)
        { Get(request, response); }

        /// <summary>
        /// Handles HTTP Post
        /// </summary>
        /// <param name="request">The request object</param>
        /// <param name="response">The response object</param>
        public override void Post(HttpListenerRequest request, HttpListenerResponse response)
        { Get(request, response); }

        /// <summary>
        /// Handles HTTP Delete
        /// </summary>
        /// <param name="request">The request object</param>
        /// <param name="response">The response object</param>
        public override void Delete(HttpListenerRequest request, HttpListenerResponse response)
        { Get(request, response); }
    }
}
